/**
 *	Copyright (c) 2009, SpLib.net
 * 	All rights reserved.
  	
 * 	Author:6spring
 * 	About:多帧图片动画
*/

package SpLib.display
{
	import SpLib.Com.Com;
	import SpLib.Com.SpLibEvent;
	import SpLib.Com.atoi;
	import SpLib.ObjSys.BaseObj;
	
	import flash.display.BitmapData;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	public class Ani extends SpLib.display.Wnd
	{
		private var m_ResMgr:ResMgr = ResMgr.GetInstance();
		private var m_ImgData:BitmapData = null;
		private var m_nCurFrame:int = -1;
		private var m_szFrameRect:Array = null;
		
		private var m_nBegFrame:int = 0;
		private var m_nEndFrame:int = 0;
		private var m_PlayTimer:flash.utils.Timer = null;
		private var m_sPlayEndEve:String;
			
		public function Ani(father:BaseObj = null)
		{
			super(father);
			
			RegEvent("Play",EVE_PlayAni);
			RegEvent("Stop",EVE_StopAni);
		}
		
		public function AttachImg(img:BitmapData):void
		{
			m_ImgData = img;
			m_szFrameRect = new Array(1);
			m_szFrameRect[0] = img.rect.clone();
			
			width = img.width;
			height = img.height;
			
			m_nCurFrame = -1;
			SetCurFrame(0);
		}
		
		public function DetachImg():void
		{
			StopAni();
			
			if(m_ImgData!=null)
			{
				m_ImgData = null;
			}
			
			m_szFrameRect = null;
			m_nCurFrame = -1;
		}
		
		public function SetSrc(pakname:String,filename:String):Boolean
		{
			DetachImg();
			
			var extstr:String = "";
			var extpos:int = filename.indexOf("#");
			if(extpos>0)
			{
				extstr = filename.substr(extpos+1);
				filename = filename.substr(0,extpos);
			}
			
			m_ImgData = m_ResMgr.GetBitmapData(pakname,filename);
			MakeFrameRect(extstr);
			SetCurFrame(0);
	
			return m_ImgData!=null;
		}
		
		private function MakeFrameRect(extstr:String):void
		{
			if(m_ImgData==null) return;
			
			if(extstr.length==0)
			{
				m_Rect.width = m_ImgData.width;
				m_Rect.height = m_ImgData.height;
				
				m_szFrameRect = new Array(1);
				m_szFrameRect[0] = new Rectangle(0,0,m_ImgData.width,m_ImgData.height);
				return;
			}
			
			var hframe:int = 0;
			var vframe:int = 0;
			var fw:int = 0;
			var fh:int = 0;
			var x:int = 0;
			var y:int = 0;
			
			var rectinfo:Array = extstr.split(",");
			switch(rectinfo.length)
			{
			case 0:
			{
				hframe = 1;
				vframe = 1;
				
				fw = m_ImgData.width;
				fh = m_ImgData.height;
			}
			break;
			case 2:
			{
				vframe = SpLib.Com.atoi( rectinfo[0] );
				hframe = SpLib.Com.atoi( rectinfo[1] );
				
				fw = m_ImgData.width/vframe;
				fh = m_ImgData.height/hframe;
			}
			break;
			case 4:
			{
				vframe = SpLib.Com.atoi( rectinfo[0] );
				hframe = SpLib.Com.atoi( rectinfo[1] );

				fw = SpLib.Com.atoi( rectinfo[2] );
				fh = SpLib.Com.atoi( rectinfo[3] );
			}
			break;
			case 6:
			{
				vframe = SpLib.Com.atoi( rectinfo[0] );
				hframe = SpLib.Com.atoi( rectinfo[1] );

				fw = SpLib.Com.atoi( rectinfo[2] );
				fh = SpLib.Com.atoi( rectinfo[3] );
				
				x = SpLib.Com.atoi( rectinfo[4] );
				y = SpLib.Com.atoi( rectinfo[5] );
			}
			break;
			}		
			
			width = fw;
			height = fh;
			
			m_szFrameRect = new Array(hframe*vframe);
				
			var i:int = 0;
			var j:int = 0;
			for(i=0;i<hframe;i++)
			{
				for(j=0;j<vframe;j++)
				{
					m_szFrameRect[i*vframe+j] = new Rectangle(x+j*fw,y+i*fh,fw,fh);
				}
			}
		}
		
		private function OnTimer(evt:TimerEvent):void
		{
			var frame:int = m_nCurFrame + 1;
			if(m_nEndFrame==-1)
			{
				if(frame>=m_szFrameRect.length)
				{
					frame = m_nBegFrame;
				}
			}
			else
			{
				if(frame>=m_nEndFrame)
				{
					if(m_sPlayEndEve.length!=0)
					{
						Event(m_sPlayEndEve);
					}
					
					StopAni();
				}
			}
			
			SetCurFrame(frame);
		}
		
		public function SetCurFrame(frame:int):void
		{
			if(m_ImgData==null)
			{
				return;
			}
			
			if(frame!=m_nCurFrame && frame<m_szFrameRect.length)
			{
				m_nCurFrame = frame;
			}
		}
		
		override protected function OnDrawSelf(dc:SpLib.display.Graphics2D):Boolean
		{
			if(m_szFrameRect!=null)
			{
				var rect:Rectangle = m_szFrameRect[m_nCurFrame] as Rectangle;
				dc.Draw_Normal_Alpha(m_ImgData,alpha,0,0,rect);
			}
			
			return false;
		}
		
		public function get framemax():int
		{
			return m_szFrameRect.length;
		}
		
		public function get playing():Boolean
		{
			return m_PlayTimer!=null;
		}
		
		public function PlayAni(beg:int = 0,end:int = -1,delay:int = 20,playendeve:String = ""):void
		{
			StopAni();
			
			m_nBegFrame = beg<0?0:beg;
			m_nBegFrame = m_nBegFrame>=m_szFrameRect.length?(m_szFrameRect.length-1):m_nBegFrame;
			
			m_nEndFrame = end;
			m_nEndFrame = m_nEndFrame>=m_szFrameRect.length?(m_szFrameRect.length-1):m_nEndFrame;
			
			m_sPlayEndEve = playendeve;
			
			SetCurFrame(beg);
			
			if(m_nBegFrame==m_nEndFrame)
			{
				return;
			}
				
			m_PlayTimer = new flash.utils.Timer(delay);
			m_PlayTimer.addEventListener(TimerEvent.TIMER,OnTimer);
			m_PlayTimer.start();
		}
		
		public function StopAni():void
		{
			if(m_PlayTimer!=null)
			{
				m_PlayTimer.stop();
				m_PlayTimer = null;
			}
			
			m_nBegFrame = 0;
			m_nEndFrame = -1;
		}
		
		private function EVE_PlayAni(evt:SpLibEvent):void
		{
			var beg:int,end:int,delay:int,playendeve:String;		
			beg = SpLib.Com.Com.GetArraySub(evt.m_Agv,0,"int",0);
			end = SpLib.Com.Com.GetArraySub(evt.m_Agv,1,"int",-1);
			delay = SpLib.Com.Com.GetArraySub(evt.m_Agv,2,"int",20);
			playendeve = SpLib.Com.Com.GetArraySub(evt.m_Agv,2,"string","");
		
			PlayAni(beg,end,delay,playendeve);
		}
		
		private function EVE_StopAni(evt:SpLibEvent):void
		{
			StopAni();
		}
	}
}